package com.lzj.demo.controller;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.lzj.demo.dto.Address;
import com.lzj.demo.dto.Role;
import com.lzj.demo.dto.User;
import com.lzj.demo.dto.UserDTO;
import com.lzj.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController(value = "userControllerA")
@RequestMapping("/user")
public class UserController {
    /**
     * 注入 Map 检索器，它检索出来的数据以 Map 对象呈现
     */
    @Resource
    private MapSearcher mapSearcher;

    /**
     * 注入 Bean 检索器，它检索出来的数据以 泛型 对象呈现
     */
    @Resource
    private BeanSearcher beanSearcher;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/index")
    public SearchResult<Map<String, Object>> index(HttpServletRequest request) {
        // 一行代码，实现一个用户检索接口（MapUtils.flat 只是收集前端的请求参数）
        return mapSearcher.search(User.class, MapUtils.flat(request.getParameterMap()));
    }

    @GetMapping("/list")
    public List<User> list(HttpServletRequest request) {
        // 执行一条 SQL，默认查询 15 条数据
        //List<User> users = beanSearcher.searchList(User.class, null);

        //查询第 0 页，每页 1 条
        Map<String, Object> params = MapUtils.builder()
                .page(0, 1)
                .build();
        // 执行一条 SQL，默认查询 15 条数据
        List<User> users = beanSearcher.searchList(User.class, params);
        return users;
    }

    @GetMapping("/detail/{userId}")
    public User detail(@PathVariable("userId") Long id) {

//        MPJLambdaWrapper<User> wrapper = JoinWrappers.lambda(User.class)
//                .selectSub(User.class, w -> w.select(User::getId)
//                        .eq(User::getId, User::getId)
//                        .last("limit 1"), User::getId)
//                .leftJoin(Address.class, Address::getUserId, User::getId)
//                .le(User::getId, 100);
//        wrapper.list();


        MPJLambdaWrapper<User> mpjLambdaWrapper = new MPJLambdaWrapper<User>()
                //查询指定类的全部字段
                .selectAll(User.class)
                //别名
                .selectAs(Role::getName,User::getRoleName)
                //sql函数
                .selectSum(User::getPoint, UserDTO::getTotal)//SUM(t.id) AS total
                //一对多查询
                //.selectCollection(Address.class, UserDTO::getAddressList)
//                .selectCollection(Address.class, UserDTO::getAddressList, map -> map
//                        //此处只能映射 AddressDO.class 中的字段到 UserDTO::getAddressList 中
//                        .id(Address::getId)
//                        .result(Address::getAddress)
//                        //别名映射
//                        .result(Address::getAddress, AddressDTO::getAddress))
                //左连接
                .leftJoin(Role.class, Role::getUserId, User::getId)
                //.leftJoin(Address.class, Address::getUserId, User::getId)
                .eq(User::getId,id);
        List<User> list = userMapper.selectJoinList(User.class, mpjLambdaWrapper);
        return list.get(0);
    }
}
